package com.gouwudai.gobbs.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.extremecomponents.table.limit.Limit;

import com.gouwudai.gobbs.common.AjaxReturn;
import com.gouwudai.gobbs.common.Constants;
import com.gouwudai.gobbs.common.IPageList;
import com.gouwudai.gobbs.dao.IClassDAO;
import com.gouwudai.gobbs.dao.ILogInfoDAO;
import com.gouwudai.gobbs.dao.IMessageDAO;
import com.gouwudai.gobbs.dao.IRuleDAO;
import com.gouwudai.gobbs.dao.ITopicContentDAO;
import com.gouwudai.gobbs.dao.ITopicDAO;
import com.gouwudai.gobbs.dao.ITopicReDAO;
import com.gouwudai.gobbs.dao.IUserDAO;
import com.gouwudai.gobbs.pojo.TbbsClass;
import com.gouwudai.gobbs.pojo.TbbsMessage;
import com.gouwudai.gobbs.pojo.TbbsRule;
import com.gouwudai.gobbs.pojo.TbbsTopic;
import com.gouwudai.gobbs.pojo.TbbsTopicContent;
import com.gouwudai.gobbs.pojo.TbbsTopicRe;
import com.gouwudai.gobbs.pojo.TbbsUserInfo;
import com.gouwudai.gobbs.service.ITopicService;
import com.litt.core.common.PageParam;
import com.litt.core.common.Utility;
import com.litt.core.exception.BusiException;
import com.litt.core.exception.DBException;
import com.litt.core.util.ExtremeTableUtil;
import com.litt.core.util.ParamUtils;

public class TopicServiceImpl implements ITopicService
{
    private transient final Log logger = LogFactory.getLog(TopicServiceImpl.class);
    
    ITopicDAO topicDAO;
	IRuleDAO ruleDAO;
	IUserDAO userDAO;
	IClassDAO classDAO;	
	ITopicReDAO topicReDAO;
	ITopicContentDAO topicContentDAO;
	IMessageDAO messageDAO;	//短消息服务	
    ILogInfoDAO logInfoDAO; //日志
	
	/**
	 * 新建主题
	 * @param paramMap
	 * @return
	 */
	public String saveTopic(HashMap paramMap)
	{	
			int classId = ParamUtils.getMapToInt(paramMap, "classId");
			int parentId = ParamUtils.getMapToInt(paramMap, "parentId");
			int grandId = ParamUtils.getMapToInt(paramMap, "grandId");			
			
			int userId = ParamUtils.getMapToInt(paramMap, "userId");
			String loginId = ParamUtils.getMapToString(paramMap, "loginId");
			String topicAddIp = ParamUtils.getMapToString(paramMap, "topicAddIp");
			
			String title = ParamUtils.getMapToString(paramMap, "title");
			String topicStyleList = ParamUtils.getMapToString(paramMap, "topicStyleList");
			int topicMood = ParamUtils.getMapToInt(paramMap, "topicMood");
			String content = ParamUtils.getMapToString(paramMap, "content");
			int contentLen = content.length();
			
			TbbsTopic tbbsTopic = new TbbsTopic();
			TbbsTopicRe tbbsTopicRe = new TbbsTopicRe();
			TbbsTopicContent tbbsTopicContent = new TbbsTopicContent();
			
			tbbsTopic.setTitle(title);
			tbbsTopic.setUserId(userId);
			tbbsTopic.setLoginId(loginId);
			tbbsTopic.setClassId(classId);
			tbbsTopic.setClassParentId(ParamUtils.getMapToInt(paramMap, "classParentId"));
			tbbsTopic.setTopicStyleList(topicStyleList);
			tbbsTopic.setTopicMood(topicMood);
			tbbsTopic.setTopicPro(0);
			tbbsTopic.setTopicType(ParamUtils.getMapToInt(paramMap, "topicType"));
			tbbsTopic.setTopicTypeNum(ParamUtils.getMapToBigDecimal(paramMap, "topicTypeNum"));
			tbbsTopic.setTopicAddTime(new Date());
			tbbsTopic.setTopicAddIp(topicAddIp);		
			tbbsTopic.setTopicLen(contentLen);
			tbbsTopic.setTotalStorey(0);//总楼层为0
			tbbsTopic.setTopicIsPass(ParamUtils.getMapToBoolean(paramMap, "topicIsPass"));	//从系统属性中获取主题是否需要审核
            tbbsTopic.setTopicReCount(new Integer(0));
            tbbsTopic.setTopicViewCount(new Integer(0));
            tbbsTopic.setTopicOnTop(new Integer(0));
            tbbsTopic.setTopicIsDel(false);
            
			tbbsTopicRe.setClassId(classId);
			tbbsTopicRe.setReOrder(1);	//设置主题的查询排序为1
			tbbsTopicRe.setStorey(0);// 新增主题对应的回复楼层为0
			tbbsTopicRe.setTitle(title);
			tbbsTopicRe.setUserId(userId);
			tbbsTopicRe.setLoginId(loginId);
			tbbsTopicRe.setReStyleList(topicStyleList);
			tbbsTopicRe.setReMood(topicMood);
			tbbsTopicRe.setRePro(0);
			tbbsTopicRe.setReAddTime(new Date());
			tbbsTopicRe.setReAddIp(topicAddIp);
			tbbsTopicRe.setReLen(contentLen);
			
			tbbsTopicContent.setContent(content);	
			tbbsTopicContent.setContentUserPen(ParamUtils.getMapToBoolean(paramMap, "contentUserPen"));			
			tbbsTopicContent.setContentEmail(ParamUtils.getMapToBoolean(paramMap, "contentEmail"));
			tbbsTopicContent.setContentCopyright(ParamUtils.getMapToInt(paramMap, "contentCopyright"));	
            tbbsTopicContent.setIsRicheditor(new Integer(1));   //是否富文本编辑器
			
			Integer topicId = topicDAO.saveTopic(tbbsTopic,tbbsTopicRe,tbbsTopicContent);
			
			//读取社区规则，给当前用户增加积分			
			TbbsRule tbbsRule = ruleDAO.loadByCode("R001");	//发表帖子的规则
			BigDecimal ruleMark = tbbsRule.getRuleMark();
			BigDecimal ruleMoney = tbbsRule.getRuleMoney();
			BigDecimal ruleCredit = tbbsRule.getRuleCredit();
			//BigDecimal ruleDeposit = tbbsRule.getRuleDeposit();			
			
			userDAO.topicNewOrUpdate(userId,ruleMoney,ruleMark,ruleCredit,false);			
			//保存成功后还要更新当前版面的统计信息
				
			classDAO.updateTopicAdd(classId,parentId,grandId);            
			String ret = AjaxReturn.buildAjaxXml("topicId", "int", topicId);
            return ret;
	}
	
	/**
	 * 回复主题
	 * @param paramMap
	 */
	public void doReTopic(HashMap paramMap)
	{	
			int classId = ParamUtils.getMapToInt(paramMap, "classId");
			int parentId = ParamUtils.getMapToInt(paramMap, "parentId");
			int grandId = ParamUtils.getMapToInt(paramMap, "grandId");			
			
			int topicId = ParamUtils.getMapToInt(paramMap, "topicId");
			int topicReId = ParamUtils.getMapToInt(paramMap, "topicReId");	//暂时没用
			//int storey = ParamUtils.getMapToInt(paramMap, "storey");
			
			int userId = ParamUtils.getMapToInt(paramMap, "userId");
			String loginId = ParamUtils.getMapToString(paramMap, "loginId");
			String topicAddIp = ParamUtils.getMapToString(paramMap, "topicAddIp");
			
			String title = ParamUtils.getMapToString(paramMap, "title");
			String topicStyleList = ParamUtils.getMapToString(paramMap, "topicStyleList");
			int topicMood = ParamUtils.getMapToInt(paramMap, "topicMood");
			String content = ParamUtils.getMapToString(paramMap, "content");
			int contentLen = content.length();
						
			TbbsTopic tbbsTopic = load(topicId);
			TbbsTopicRe tbbsTopicRe = new TbbsTopicRe();
			TbbsTopicContent tbbsTopicContent = new TbbsTopicContent();
			
			int storey = tbbsTopic.getTotalStorey().intValue()+1;
			tbbsTopic.setTotalStorey(storey);
			tbbsTopic.setTopicLastReTime(new Date());
			tbbsTopic.setTopicLastReUser(loginId);	
			tbbsTopic.setTopicReCount(tbbsTopic.getTopicReCount().intValue()+1);	//修改回复的统计信息			
			
			tbbsTopicRe.setTopicId(topicId);
			tbbsTopicRe.setClassId(classId);
			tbbsTopicRe.setStorey(storey);// 回复楼层为当前总楼层+1
			tbbsTopicRe.setTitle(title);
			tbbsTopicRe.setUserId(userId);
			tbbsTopicRe.setLoginId(loginId);
			tbbsTopicRe.setReStyleList(topicStyleList);
			tbbsTopicRe.setReMood(topicMood);
			tbbsTopicRe.setRePro(0);
			tbbsTopicRe.setReAddTime(new Date());
			tbbsTopicRe.setReAddIp(topicAddIp);
			tbbsTopicRe.setReLen(contentLen);
			
			tbbsTopicContent.setTopicId(topicId);
			tbbsTopicContent.setContent(content);	
			tbbsTopicContent.setContentUserPen(ParamUtils.getMapToBoolean(paramMap, "contentUserPen"));
			tbbsTopicContent.setContentEmail(ParamUtils.getMapToBoolean(paramMap, "contentEmail"));
			tbbsTopicContent.setContentCopyright(ParamUtils.getMapToInt(paramMap, "contentCopyright"));	
            tbbsTopicContent.setIsRicheditor(new Integer(1));   //是否富文本编辑器
			
			topicDAO.doReTopic(tbbsTopic,tbbsTopicRe,tbbsTopicContent);
			
			//读取社区规则，给当前用户增加积分			
			TbbsRule tbbsRule = ruleDAO.loadByCode("R002");	//回复帖子的规则
			BigDecimal ruleMark = tbbsRule.getRuleMark();
			BigDecimal ruleMoney = tbbsRule.getRuleMoney();
			BigDecimal ruleCredit = tbbsRule.getRuleCredit();
			BigDecimal ruleDeposit = tbbsRule.getRuleDeposit();		
			
			userDAO.topicNewOrUpdate(userId,ruleMoney,ruleMark,ruleCredit,true);
			
			//保存成功后还要更新当前版面的统计信息				
			classDAO.updateTopicAdd(classId,parentId,grandId);		
	}
	
	/**
	 * 编辑主题或回复
	 * @param paramMap
	 */
	public void doEditTopic(HashMap paramMap) throws BusiException
	{	
			if(logger.isDebugEnabled())
            {
                logger.debug(paramMap);
            }
            int topicId = ParamUtils.getMapToInt(paramMap, "topicId");
			int topicReId = ParamUtils.getMapToInt(paramMap, "topicReId");	
			boolean isTopicFlag = ParamUtils.getMapToBoolean(paramMap, "isTopicFlag");			
			
			String topicEditIp = ParamUtils.getMapToString(paramMap, "topicEditIp");
			
			String title = ParamUtils.getMapToString(paramMap, "title");
			String topicStyleList = ParamUtils.getMapToString(paramMap, "topicStyleList");
			int topicMood = ParamUtils.getMapToInt(paramMap, "topicMood");
			String content = ParamUtils.getMapToString(paramMap, "content");
			int contentLen = content.length();
			
			TbbsTopic tbbsTopic = null;		
			TbbsTopicRe tbbsTopicRe = topicReDAO.load(new Integer(topicReId));
			TbbsTopicContent tbbsTopicContent = topicContentDAO.loadByTopicReId(new Integer(topicReId));
			if(tbbsTopicRe==null)
                throw new BusiException("信息丢失，请联系管理员！");
            if(tbbsTopicContent==null)
                throw new BusiException("信息丢失，请联系管理员！");
            
			if(isTopicFlag)	//如果当前编辑的是发表的主题
			{
				tbbsTopic = load(topicId);	
				tbbsTopic.setTitle(title);
				tbbsTopic.setTopicLen(contentLen);
				tbbsTopic.setTopicStyleList(topicStyleList);
				tbbsTopic.setTopicMood(topicMood);	
				tbbsTopic.setLastEditTime(new Date());
				tbbsTopic.setLastEditIp(topicEditIp);
			}			
			
			tbbsTopicRe.setTitle(title);			
			tbbsTopicRe.setReStyleList(topicStyleList);
			tbbsTopicRe.setReMood(topicMood);
			tbbsTopicRe.setLastEditTime(new Date());
			tbbsTopicRe.setLastEditIp(topicEditIp);
			tbbsTopicRe.setReLen(contentLen);			
			
			tbbsTopicContent.setContent(content);	
			tbbsTopicContent.setContentUserPen(ParamUtils.getMapToBoolean(paramMap, "contentUserPen"));
			tbbsTopicContent.setContentEmail(ParamUtils.getMapToBoolean(paramMap, "contentEmail"));
			tbbsTopicContent.setContentCopyright(ParamUtils.getMapToInt(paramMap, "contentCopyright"));	
            tbbsTopicContent.setIsRicheditor(new Integer(1));   //是否富文本编辑器
			
			topicDAO.doEditTopic(tbbsTopic,tbbsTopicRe,tbbsTopicContent);	
	}	
	
	/**
	 * 逻辑删除主题或回复
	 * @param paramMap
	 */
	public void deleteTopicLogic(HashMap paramMap)
	{	
			int topicId = ParamUtils.getMapToInt(paramMap, "topicId");
			int topicReId = ParamUtils.getMapToInt(paramMap, "topicReId");
			String title = ParamUtils.getMapToString(paramMap, "title");
			int topicUserId = ParamUtils.getMapToInt(paramMap, "topicUserId");	//帖子的作者
			String userLoginId = ParamUtils.getMapToString(paramMap, "userLoginId");	//当前用户
			String userLoginIp = ParamUtils.getMapToString(paramMap, "userLoginIp");	//当前用户登陆IP
			String topicReUserLoginId = ParamUtils.getMapToString(paramMap, "topicReUserLoginId");	//删除的主题或回复的作者
			boolean sendMessageFlag = ParamUtils.getMapToBoolean(paramMap, "sendMessageFlag");
			String message = ParamUtils.getMapToString(paramMap, "message");	//发送短消息
			boolean isTopicFlag = ParamUtils.getMapToBoolean(paramMap, "isTopicFlag");			
			if(isTopicFlag)	//逻辑删除主题
			{
				topicDAO.deleteLogic(String.valueOf(topicId));
				//读取社区规则信息				
				TbbsRule tbbsRule = ruleDAO.loadByCode("R005");	//删除主题的规则
				BigDecimal ruleMark = tbbsRule.getRuleMark();
				BigDecimal ruleMoney = tbbsRule.getRuleMoney();
				BigDecimal ruleCredit = tbbsRule.getRuleCredit();				
				userDAO.doBusinessUpdate(topicUserId, ruleMoney, ruleMark, ruleCredit, new BigDecimal(0));				
			}	
			else  //逻辑删除回复
			{				
				topicReDAO.deleteLogic(topicReId);				
			}	
			if(sendMessageFlag)	//发送短消息
			{
				message = userLoginId+"在"+Utility.getCurrentDateTime()+"删除了您发布的主题【"+title+"】<br>"+message;
					
				TbbsMessage tbbsMessage = new TbbsMessage();
				tbbsMessage.setSendUser(userLoginId);
				tbbsMessage.setTakeUser(topicReUserLoginId);
				tbbsMessage.setTitle("系统信息：主题删除");
				tbbsMessage.setOpIp(userLoginIp);
				tbbsMessage.setOpTime(new Date());
				tbbsMessage.setSendIp(userLoginIp);
				tbbsMessage.setSendTime(new Date());
				tbbsMessage.setSendFlag(0);	//直接发送
				tbbsMessage.setContent(message);	
				tbbsMessage.setReadFlag(false);
				messageDAO.save(tbbsMessage);
            }    
	}
	
	public TbbsTopic load(Integer id)
	{
		return (TbbsTopic)topicDAO.load(id);
	}

    /**
	 * 主题表的浏览次数累加
	 * @param topicId
	 * @throws DBException
	 */
	public void updateTopicViewCount(Integer topicId)
	{
			topicDAO.updateTopicViewCount(topicId);
	}

	/**
	 * 主题表的回复次数累加
	 * @param topicId
	 * @param lastReUser
	 * @param lastReTime
	 * @throws DBException
	 */
	public void updateTopicReCount(Integer topicId, String lastReUser,
			Date lastReTime)
	{
			topicDAO.updateTopicReCount(topicId, lastReUser, lastReTime);
	}


	/**
	 * 修改主题类型
	 * @param topicId
	 * @throws DBException
	 */
	public void updateTopicPro(Integer topicPro, Integer topicId)
	{
			topicDAO.updateTopicPro(topicPro, topicId);
	}
    
    /**
     * 主题管理.
     * 
     * @param paramMap 参数映射
     */
    public void doTopicManage(Map paramMap)
    {
        String action = ParamUtils.getMapToString(paramMap, "action"); //操作类型        
        int userId = ParamUtils.getMapToInt(paramMap, "userId"); //用户ID
        int classId = ParamUtils.getMapToInt(paramMap, "classId"); //版面ID  
        int topicId = ParamUtils.getMapToInt(paramMap, "topicId"); //主题ID
        String topicName = ParamUtils.getMapToString(paramMap, "topicName"); //信誉分
        BigDecimal userCredit = ParamUtils.getMapToBigDecimal(paramMap, "userCredit"); //信誉分
        BigDecimal userMark = ParamUtils.getMapToBigDecimal(paramMap, "userMark"); //积分
        BigDecimal userMoney = ParamUtils.getMapToBigDecimal(paramMap, "userMoney"); //金币
        String reason = ParamUtils.getMapToString(paramMap, "reason"); //操作理由
        Boolean msgFlag = ParamUtils.getMapToBoolean(paramMap, "msgFlag"); //是否发送短消息
        int opUserId = ParamUtils.getMapToInt(paramMap, "opUserId"); //操作员
        String opUserLoginId = ParamUtils.getMapToString(paramMap, "opUserLoginId"); //操作员登陆ID
        String opIp = ParamUtils.getMapToString(paramMap, "opIp"); //操作员IP
        
        int topicOnTop = ParamUtils.getMapToInt(paramMap, "opUserId"); //置顶类型
        int moveClassId = ParamUtils.getMapToInt(paramMap, "moveClassId"); //移动前的主题所在版面
        Boolean saveLink = ParamUtils.getMapToBoolean(paramMap, "saveLink"); //是否在原版面保留链接
        
        String actionName = "";  //操作类型名称 
        if(action.equals("best")){
            actionName = "加为精品";
            topicDAO.updateTopicPro(new Integer(3), topicId);
        }else if(action.equals("nobest")){  
            actionName = "解除精品";
            topicDAO.updateTopicPro(new Integer(0), topicId);     
        }else if(action.equals("lock")){
            actionName = "锁定主题";
            topicDAO.updateTopicPro(new Integer(1), topicId);  
        }else if(action.equals("unlock")){
            actionName = "解锁主题";
            topicDAO.updateTopicPro(new Integer(1), topicId);           
        }else if(action.equals("move")){
            actionName = "移动主题";   
            TbbsClass moveClass = classDAO.load(moveClassId);
            topicDAO.doMoveTopic(topicId, moveClassId, moveClass.getParentId(), opUserLoginId, saveLink);
        }else if(action.equals("top")){
            actionName = "置顶主题";
            topicDAO.updateTopicOnTop(topicOnTop, topicId);
        }else{
            actionName = "主题加(扣)分";            
        }
        //给主题作者加(减)分
        this.topicNum(actionName, userId, classId, topicId, topicName, userCredit, userMark, userMoney, reason, msgFlag, opUserId,opUserLoginId ,opIp);
    }
    
    
    /**
     * 给主题作者加(减)分.
     * 
     * @param opUserId 操作员
     * @param classId 版面ID
     * @param userId 用户ID
     * @param actionName 操作类型名称
     * @param userMark 积分操作
     * @param topicName 主题名称
     * @param opUserLoginId 登陆操作员LOGIN_ID
     * @param reason 操作理由
     * @param userMoney 金币操作
     * @param topicId 主题ID
     * @param opIp 操作员IP
     * @param msgFlag 是否发送短消息
     * @param userCredit 信誉操作
     */
    private void topicNum(String actionName,int userId,int classId,int topicId,String topicName,BigDecimal userCredit,BigDecimal userMark,BigDecimal userMoney,String reason,boolean msgFlag,int opUserId,String opUserLoginId,String opIp)
    {   
        //TbbsUserInfo opUserInfo = userDAO.load(opUserId);    //读取操作用户信息
        TbbsUserInfo userInfo = userDAO.load(userId);   //读取主题作者信息  
        if(userCredit.doubleValue()!=0||userMark.doubleValue()!=0||userMoney.doubleValue()!=0)
        {   
            userDAO.doBusinessUpdate(userId, userMoney, userMark, userCredit, new BigDecimal(0));
            
            //构造日志内容 START
            StringBuffer opContent = new StringBuffer(200);
            opContent.append(opUserLoginId);
            opContent.append("于");
            opContent.append(Utility.getCurrentDateTime());
            opContent.append("对【");
            opContent.append(topicName);
            opContent.append("】进行了");
            opContent.append(actionName);            
            opContent.append("操作。信誉分：");
            opContent.append(userCredit);
            opContent.append("，积分：");
            opContent.append(userMark);
            opContent.append("，金币：");
            opContent.append(userMoney);
            opContent.append("。操作理由：");
            opContent.append(reason);
            //构造日志内容 END
            logInfoDAO.saveTopicLog(opUserLoginId, opIp, classId, topicId, 4, opContent.toString());
        } 
        if(msgFlag) //给用户发送短消息通知
        {
            TbbsMessage tbbsMessage = new TbbsMessage();
                   
            tbbsMessage.setSendUser(opUserLoginId);
            
            tbbsMessage.setTakeUser(userInfo.getLoginId());
            tbbsMessage.setTitle("系统信息："+actionName);
            
            tbbsMessage.setOpIp(opIp);
            tbbsMessage.setOpTime(new Date());
            tbbsMessage.setSendIp(opIp);
            tbbsMessage.setSendTime(new Date());
            tbbsMessage.setSendFlag(0); //直接发送
            //拼接发送消息的内容
            StringBuffer message = new StringBuffer(100);
            message.append("版主(");
            message.append(opUserLoginId);
            message.append(")于");
            message.append(Utility.getCurrentDateTime());
            message.append("执行了");
            message.append(actionName);            
            message.append("操作,相关主题【");
            message.append(topicName);
            message.append("】。");
            if(userCredit.doubleValue()!=0)
            {  
                message.append("信誉分");
                message.append(userCredit);
                message.append("，");
            }            
            if(userMark.doubleValue()!=0)
            {
                message.append("积分");
                message.append(userMark);
                message.append("，");
            }
            
            if(userMoney.doubleValue()!=0)
            {
                message.append("金币");
                message.append(userMoney);
                message.append("，");
            }            
            message.append("操作原因：");
            message.append(reason);
            //拼接发送消息的内容结束
            tbbsMessage.setContent(message.toString());    
            tbbsMessage.setReadFlag(false);
            messageDAO.save(tbbsMessage);
        }    
    }    
    
    /**
     * 审核多个主题
     * @param topicIds 用逗号分隔好的topicId
     */
    public void doCheckTopic(String topicIds)
    {
        if(!Utility.isEmpty(topicIds))
        {
            String[] ids = Utility.spitString(topicIds, ",");
            
            for(int i=0;i<ids.length;i++)
            {
                topicDAO.doCheckTopic(Utility.parseInt(ids[i]));
            }    
        }
    }

	/**
	 * 逻辑删除主题
	 * @param topicIds 用逗号分隔好的topicId
	 */
	public void deleteLogic(String topicIds)
	{
		if(!Utility.isEmpty(topicIds))
		    topicDAO.deleteLogic(topicIds);
	}

	/**
	 * 恢复已删除主题
     * @param topicIds 用逗号分隔好的topicId
	 */
	public void doResume(String topicIds)
	{
        if(!Utility.isEmpty(topicIds))
            topicDAO.doResume(topicIds);
	}
    
    /**
     * 物理删除主题
     * @param topicIds 用逗号分隔好的topicId
     */
    public void delete(String topicIds)
    {
        if(!Utility.isEmpty(topicIds))
        {
            String[] ids = Utility.spitString(topicIds, ",");
            
            for(int i=0;i<ids.length;i++)
            {
                topicDAO.delete(Utility.parseInt(ids[i]));
            }    
        }
    }    

    
    /**
     * 物理删除主题
     * @param topicIds
     */
    public void delete(String[] topicIds)
    {
        if(topicIds!=null&&topicIds.length>0)
        {
            for(int i=0;i<topicIds.length;i++)
            {
                topicDAO.delete(Utility.parseInt(topicIds[i]));
            }    
        }
    }    


    /**
     * 查询当前版面的主题信息
     * @param classId
     * @param pageIndex
     * @param pageSize
     * @return
     */
    public List listByClass(Integer classId,String menuStyle,int pageIndex,int pageSize)
    {
    	Map<String,Object> conds = new HashMap<String,Object>();
        conds.put("classId", classId);
        conds.put("menuStyle", menuStyle);        
        return topicDAO.listByClass(new PageParam(conds, pageIndex, pageSize));
    }
    
    /**
     * 查询当前版面的主题信息数量（供分页）
     * @param classId
     * @param pageIndex
     * @param pageSize
     * @return
     */
    public int listCountByClass(Integer classId,String menuStyle)
    {
        Map<String,Object> conds = new HashMap<String,Object>();
        conds.put("classId", classId);
        conds.put("menuStyle", menuStyle);   
        return topicDAO.listCountByClass(new PageParam(conds,0,0));    	
    }
    
    /**
     * 根据主题属性查询
     * @param topicPro
     * @param size
     * @return
     */
    public List listByPro(Integer topicPro,int size)
    {
    	return topicDAO.listByPro(topicPro, size);    	
    }
	
	public IPageList listPage(PageParam pageParam)
	{
		return topicDAO.listPage(pageParam);
	}

    
    public void AutoBindingTable(javax.servlet.http.HttpServletRequest request)
    {
        Limit limit = ExtremeTableUtil.getLimit(request, Constants.DEFAULT_PAGE_SIZE);
        IPageList page = this.listPage(new PageParam(limit.getPage(),limit.getCurrentRowsDisplayed(),ExtremeTableUtil.getSort(limit)));
        request.setAttribute("resultList",page.getRsList());
        request.setAttribute("totalRows",page.getTotalSize());
    } 
    
    /**
     * 后台文章管理表格查询.
     * 查询条件：查询类型(listType):0-全部，1-已认证主题，2-未认证主题，3-回收站主题
     *         主题名称(title)
     *         所在版面(classId)
     *         主题类型(topicType)
     * @param request
     */
    public void listManagePage(javax.servlet.http.HttpServletRequest request)
    {
        Map<String,Object> conds = new HashMap<String,Object>();
        conds.put("listType", request.getParameter("listType"));    //查询类型:0-全部，1-已认证主题，2-未认证主题，3-回收站主题
        conds.put("title", request.getParameter("title"));
        conds.put("classId", Utility.parseInt(request.getParameter("classId")));
        conds.put("topicType", Utility.parseInt(request.getParameter("topicType")));        
        Limit limit = ExtremeTableUtil.getLimit(request, Constants.DEFAULT_PAGE_SIZE);
        IPageList page = topicDAO.listManagePage(new PageParam(conds,limit.getPage(),limit.getCurrentRowsDisplayed(),ExtremeTableUtil.getSort(limit)));
        request.setAttribute("resultList",page.getRsList());
        request.setAttribute("totalRows",page.getTotalSize());
    }     
    
    /**
     * 回收站查询
     * @param pageParam
     * @return IPageList
     */
    public IPageList listRecycle(PageParam pageParam)
    {
        return topicDAO.listRecycle(pageParam);
    }    
    
    public void AutoBindingRecycleTable(javax.servlet.http.HttpServletRequest request)
    {
        Map<String,Object> conds = new HashMap<String,Object>();
        conds.put("listType", request.getParameter("listType"));    //查询类型:''-全部，topic-主题，re-回复        
        Limit limit = ExtremeTableUtil.getLimit(request, Constants.DEFAULT_PAGE_SIZE);
        IPageList page = this.listRecycle(new PageParam(conds,limit.getPage(),limit.getCurrentRowsDisplayed(),ExtremeTableUtil.getSort(limit)));
        request.setAttribute("resultList",page.getRsList());
        request.setAttribute("totalRows",page.getTotalSize());
    }  
  

    /**
     * 查询最新的10个主题
     * @return
     */
	public List listTop10()
	{	
		return topicDAO.listTop10();		
	}
	
	
    /**
     * 查询我发表的主题或回复
     * @param userId 用户ID
     * @param type -  0.我的主题,1.我的回复
     * @param pageIndex
     * @param pageSize
     * @return
     * @throws DBException
     */
    public List listMyTopic(int userId,int type,int pageIndex,int pageSize)
    {
    	return topicDAO.listMyTopic(userId, type, pageIndex, pageSize);
    }
    
    /**
     * 查询我发表的主题或回复
     * @param userId 用户ID
     * @param type - 0.我的主题,1.我的回复
     * @return
     * @throws DBException
     */
    public int listCountMyTopic(int userId,int type)
    {
    	return topicDAO.listCountMyTopic(userId, type);
    }

	public void setClassDAO(IClassDAO classDAO) {
		this.classDAO = classDAO;
	}

	public void setMessageDAO(IMessageDAO messageDAO) {
		this.messageDAO = messageDAO;
	}

	public void setRuleDAO(IRuleDAO ruleDAO) {
		this.ruleDAO = ruleDAO;
	}

	public void setTopicContentDAO(ITopicContentDAO topicContentDAO) {
		this.topicContentDAO = topicContentDAO;
	}

	public void setTopicDAO(ITopicDAO topicDAO) {
		this.topicDAO = topicDAO;
	}

	public void setTopicReDAO(ITopicReDAO topicReDAO) {
		this.topicReDAO = topicReDAO;
	}

	public void setUserDAO(IUserDAO userDAO) {
		this.userDAO = userDAO;
	}

    public void setLogInfoDAO(ILogInfoDAO logInfoDAO)
    {
        this.logInfoDAO = logInfoDAO;
    }

}